package com.demo.common.controller;

import java.util.Date;

import com.demo.common.model.Blog;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;

public class BlogController extends Controller {
	public void index(){
		System.out.println("执行index");
		setAttr("blogs", Blog.me.find("select * from blog"));
		render("index.jsp");
	}
	public void form(){
		Integer id=getParaToInt("id");
		if(id!=null&&id>0){
			setAttr("blog", Blog.me.findById(id));
		}
		render("form.jsp");
	}
	
	public void save(){
		UploadFile file=getFile("img");
		Blog blog=getModel(Blog.class, "blog");
		if(blog.get("id")==null){
			blog.set("publishTime", new Date());
			blog.save();
		}else if(blog.getInt("id")>0){
			blog.update();
		}
		
		index();
	}
	
	public void delete(){
		Integer id=getParaToInt("id");
		if(id!=null&&id>0){
			boolean flag=Blog.me.deleteById(id);
			if(!flag){
				renderText("删除失败!");
				return;
			}
		}else{
			renderText("删除失败!");
			return;
		}
		index();
	}
	

}
